package com.heitian.ssm.controller;

import com.heitian.ssm.Constants.WebConstants;
import com.heitian.ssm.model.AdminEntity;
import com.heitian.ssm.model.UserEntity;
import com.heitian.ssm.service.AdminLoginService;
import com.heitian.ssm.service.UserLoginService;
import com.heitian.ssm.utils.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2018-04-22
 * Time: 17:52
 * Des:
 */
@Controller
@RequestMapping("/adminlogin")
public class AdminLoginController {
  /* request.getSession().setAttribute("customers", customer);
    Customer cus = (Customer) session.getAttribute("customers");*/
  private Logger log = Logger.getLogger(AdminLoginController.class);

    @Resource
    private AdminLoginService adminLoginService;

    @RequestMapping("/showUser")
    @ResponseBody
    public void showUser(HttpServletRequest request, HttpServletResponse response, @RequestParam String userName, @RequestParam String passwd)  {
        log.info("查询所有用户信息入参"+userName);

        AdminEntity userEntity=adminLoginService.login(userName,passwd);
        if(userEntity !=null){
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.successReturn(userEntity)));
        }else {
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","登录失败")));
        }


    }
    /*, @RequestParam String username1,@RequestParam String card,@RequestParam String className,@RequestParam String email1,@RequestParam String pass*/
    @RequestMapping("/registe")
    @ResponseBody
    public void registe(HttpServletRequest request, HttpServletResponse response){
        String gonghao=request.getParameter("gonghao");
        String youxianghao=request.getParameter("youxianghao");
        String yanzheng=request.getParameter("yanzheng");
        String dianhua=request.getParameter("dianhua");
        String passwd=request.getParameter("pass");
        String yanzhengma=(String)request.getSession().getAttribute("mailnum");
        if(!yanzheng.equals(yanzhengma)){
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99991","验证码失败")));
            return;
        }
        log.info("查询所有用户信息入参"+gonghao+youxianghao+yanzheng+dianhua+passwd);

        AdminEntity userEntity=new AdminEntity();
        userEntity.setAdminName(gonghao);
        userEntity.setAdminEmail(youxianghao);
        userEntity.setAdminPhone(dianhua);
        userEntity.setPwdSalt(WebConstants.SALT);

        byte by=(byte)1;
        userEntity.setIsStatus(by);
        userEntity.setCreateTime(new Date());
        userEntity.setModifyTime(new Date());
        String md5Passwd=null;
        try {
            md5Passwd= MD5.md5(passwd,WebConstants.SALT);
        } catch (Exception e) {
            e.printStackTrace();
        }

        log.info("入参"+FastJsonUtils.toJSONString(userEntity));
        userEntity.setAdminPwd(md5Passwd);
        int i=0;
        try {
            i=adminLoginService.register(userEntity);
        }catch (Exception e){
            log.error(e);
        }

        if(i<=0){
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","插入数据失败")));
        }else {
            try {
                MailUtil.send_mail(youxianghao,"尊敬的"+gonghao+"用户：感谢您注册我们的网站");
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            JsonResultUtils.ajaxReturn(response,FastJsonUtils.toJSONString(Result.successReturn(userEntity)));
        }
    }

    @RequestMapping("/mailn")
    @ResponseBody
    public void sendMail(HttpServletRequest request, HttpServletResponse response)  {


        /*Customer cus = (Customer) session.getAttribute("customers")*/
        String email=request.getParameter("mailNo");
        log.info("邮箱发送信息入参"+email);
        int i=(int)((Math.random()*9+1)*100000);
        String mailn=i+"";
        if(request.getSession().getAttribute("mailnum") !=null){
            request.getSession().removeAttribute("mailnum");
        }
        request.getSession().setAttribute("mailnum", mailn);
        try {
            MailUtil.send_mail(email,"欢迎您！注册洗浴中心管理系统：<br/>"+"您的验证码为："+mailn);
            log.info("邮箱发送信息入参");
        } catch (MessagingException e) {
            e.printStackTrace();
        }


    /*    if(userEntity !=null){
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.successReturn(userEntity)));
        }else {
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","登录失败")));
        }*/


    }
}
